<!-- 车辆列表 -->
<template>
  <div class="dashboard-container vehicle customer-list-box">
    <el-card class="box-card">
      <!-- 车辆类型框 -->
      车辆类型：
      <el-select
        v-model="value"
        placeholder="请选择"
        style="width: 220px; height: 40px; margin-right: 70px"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <!-- 车牌号码框 -->
      车牌号码：
      <el-input
        v-model="input"
        placeholder="请输入车牌号码"
        style="width: 220px; height: 40px; margin-right: 60px"
      ></el-input>
      <!-- 搜索按钮 -->
      <el-button
        type="danger"
        style="width: 80px; height: 40px; background-color: #e15536"
        @click="search"
      >搜索</el-button>
      <!-- 重置按钮 -->
      <el-button
        style="width: 80px; height: 40px"
        @click="reset"
      >重置</el-button>
    </el-card>
    <!-- tab栏 -->
    <el-card
      class="box-card"
      style="width: 1100px; height: 50px"
    >
      <a
        ref="one"
        href="#"
        class="active"
        @click="getData"
      >全部 {{ total }}</a>
      <a
        ref="two"
        href="#"
        @click="workStatus1"
      >可用 {{ use }}</a>
      <a
        ref="the"
        href="#"
        @click="workStatus0"
      >停用 {{ unuse }}</a>
    </el-card>
    <el-card
      class="box-card"
      style="  padding-bottom: 50px;"
    >
      <!-- 新增车辆按钮 -->
      <el-button
        type="danger"
        style="width: 100px; height: 40px; background-color: #e15536"
        @click="show = true"
      >新增车辆</el-button>
      <!-- 数据表格 -->
      <el-table
        v-loading="listLoading"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="brand"
          label="序号"
        > </el-table-column>
        <el-table-column
          prop="licensePlate"
          label="车牌号码"
        >
        </el-table-column>
        <el-table-column
          prop="truckTypeName"
          label="车辆类型"
        >
        </el-table-column>
        <el-table-column
          prop="driverNum"
          label="司机数量"
        > </el-table-column>
        <el-table-column
          prop="workStatus"
          label="车辆状态"
        > </el-table-column>
        <el-table-column
          prop="deviceGpsId"
          label="GPS设备ID"
          width="140"
        >
        </el-table-column>
        <el-table-column
          prop="allowableLoad"
          label="实载重量（T）"
          width="100"
        >
        </el-table-column>
        <el-table-column
          prop="allowableVolume"
          label="实载体积（方）"
          width="100"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          width="200"
        >
          <template #default="{ row }">
            <el-button
              size="small"
              type="text"
              @click="$router.push(`vehicle-detail?id=${row.id}`)"
            >查看详情</el-button>
            |
            <el-button
              size="small"
              type="text"
              @click="dialog(row.id)"
            >启用</el-button>
            |
            <el-button
              size="small"
              type="text"
              @click="dialog2(row)"
            >配置司机</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div
        v-show="tableData && tableData.length > 0"
        class="pagination"
      >
        <div

          class="pages"
        >
          <el-pagination
            :current-page="Number(data.page)"
            :total="Number(total)"
            :page-size="Number(data.pageSize)"
            :page-sizes="[10, 20, 30, 50]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
    <!-- 新增车辆弹框 -->
    <el-dialog
      title="新增车辆"
      :visible="show"
      @close="show = false"
    >
      <el-form :model="form">
        <br />
        车辆类型：
        <el-select
          v-model="form.truckTypeId"
          placeholder="请选择"
          style="width: 440px; height: 40px; margin-right: 25px"
          props="type1"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <br />
        车牌号码：
        <el-input
          v-model="form.licensePlate"
          placeholder="请输入车牌号码"
          style="
            width: 440px;
            height: 40px;
            margin-top: 20px;
            margin-bottom: 20px;
            margin-right: 25px;
          "
          props="number1"
        ></el-input>
        <br />
        GPS设备ID:
        <el-input
          v-model="form.deviceGpsId"
          placeholder="请输入GPS设备ID"
          style="width: 440px; height: 40px; margin-right: 33px"
          props="ID1"
        ></el-input>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="show = false">取 消</el-button>
        <el-button
          type="primary"
          @click="add"
        >确 定</el-button>
      </div>
    </el-dialog>
    <!-- 启用车辆弹窗 -->
    <el-dialog
      title="车辆启用"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <span style="margin-left:-60px">确定要启用吗？车辆启用需满足以下条件：</span>
      <br>
      <br>
      <br>
      <span style="margin-left:-40px;margin-right:20px"> 1.车辆信息已完善 </span> <span> 2.绑定司机>=2,且有排班</span>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="changeType"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 配置司机弹窗 -->
    <el-dialog
      title="司机配置"
      :visible.sync="dialogVisible2"
      width="30%"
    >
      <span style="margin-left:-160px">配置司机需满足以下条件：</span>
      <br>
      <br>
      <br>
      <span style="margin-left:-40px;margin-right:20px"> 1.车辆信息已完善 </span> <span> 2.绑定司机>=2,且有排班</span>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button
          type="primary"
          @click="showTime"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 确认进行司机配置后弹窗 -->
    <el-dialog
      v-show="isShow"
      title="配置司机"
      width="640px"
      @close="isShow=false"
    >

      <div
        slot="footer"
        class="dialog-footer"
        style="text-align: center; padding-top: 30px; padding-bottom: 20px"
      >
        <div class="header">
          <div class="num">
            <label>车牌号：</label>
            <span>{{ licensePlate || '' }}</span>
          </div>
          <div class="name">
            <label>车型：</label>
            <span>{{ truckTypeName || '' }}</span>
          </div>
          <div class="carType">
            <label>车辆状态：</label>
            <span
              :class="{
                use: status === 1,
                noUse: status === 0
              }"
            >{{ status === 1 ? '可用' : '停用' }}</span>
          </div>
          <div class="agency">
            <label>实载重量：</label>
            <span>{{ allowableLoad || '' }}</span>
          </div>
          <div class="workTime">
            <label>实载体积：</label>
            <span>{{ allowableVolume || '' }}</span>
          </div>
        </div>
        <div class="serchBox">
          <label>配置司机：</label>
          <el-select
            suffix-icon="el-icon-search"
            placeholder="请输入司机名称"
            filterable
            multiple
            :value="selecteds"
            reserve-keyword
          >

          </el-select>
        </div>
        <el-table
          :key="tableKey"
          ref="multipleTable"
          v-loading="loading"
          :data="dataList"
          style="width: 100%"
          stripe
          :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
        >
          <div
            v-show="(!dataList || dataList.length <= 0) && !loading"
            slot="empty"
          >
            <img
              src="@/icons/pic-kong.png"
              alt="img"
              style="margin-top: 20px"
            />
            <p style="margin-top: -20px; padding-bottom: 0px">这里空空如也</p>
          </div>
          <el-table-column
            type="index"
            label="序号"
            width="200"
          />
          <el-table-column
            align="left"
            label="司机名称"
            prop="name"
          >
          </el-table-column>
          <el-table-column
            align="left"
            label="操作"
            width="160"
            class-name="small-padding fixed-width"
          >
          </el-table-column></el-table></div>
      <!-- 底部按钮 -->
      <div
        slot="footer"
        class="dialog-footer"
        style="text-align: center; padding-top: 30px; padding-bottom: 20px"
      >
        <el-button
          type="warning"
          style="width: 90px"
          class="save-btn"
          @click="mess"
        >确认</el-button>
        <el-button
          plain
          style="width: 90px; color: #2a2929"
          class="cancel-btn"
          @click="dialogFormH"
        >取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import { getCarTypeList, truckList, getCarTypeStatics, carArrangeDriver, getDriverListByCar } from '@/api/transit'
export default {
  name: 'Vehicle',

  data() {
    return {
      tableKey: 0,
      dialogFormVisible: false,
      dataList: [],
      selecteds: [],
      loading: false,
      id: ``,
      licensePlate: ``,
      truckTypeName: ``,
      status: ``,
      allowableLoad: ``,
      allowableVolume: '',
      dialogVisible: false,
      dialogVisible2: false,
      isShow: false,
      listLoading: true,
      activeClass: 0,
      show: false,
      list: [{ title: '全部 24' }, { title: '可用 11' }, { title: '停用 13' }],
      tableData: [],
      data: {
        page: 1,
        pageSize: 10
      },
      total: null,
      use: null,
      unuse: null,
      options: [],
      input: '',
      value: '',
      form: {
        licensePlate: '',
        deviceGpsId: '',
        truckTypeId: ''
      }
    }
  },
  computed: {

  },
  created() {
    // 数据列表
    this.getData()
    // 车型下拉框
    this.getOptions()
  },
  methods: {
    // 点击提交时拒绝操作
    mess() {
      this.$message.error(`不允许操作`)
      this.isShow = false
    },
    async getDriverListByCar (id) {
      this.loading = true
      await getDriverListByCar(id).then((res) => {
        if (String(res.code) === '200') {
          this.dataList = res.data
          this.selecteds = res.data.map((item) => item.name)
          this.loading = false
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 删除选中司机
    deleteDriver (id, name) {
      this.dataList = this.dataList.filter((item) => item.userId !== id)
      this.selecteds = this.selecteds.filter((item) => item !== name)
    },
    async confirm () {
      await carArrangeDriver({
        driverIds: this.dataList.map((item) => item.userId),
        truckId: this.carModalInfo.id
      }).then((res) => {
        if (String(res.code) === '200') {
          this.$message.success('安排司机成功')
          this.$emit('newDataes')
          this.dialogFormVisible = false
          this.dataList = []
          this.selecteds = []
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 跳转到司机详情
    handleVehicleDetail (id) {
      this.$router.push({
        path: '/transit/driver-detail',
        query: { id: id, formUrlName: '司机管理' }
      })
    },
    // 弹层显示
    dialogFormV () {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.getDriverListByCar(this._props.carModalInfo.id)
      })
    },
    // 弹层隐藏
    dialogFormH () {
      this.isShow = false
    },
    // 修改启用停用弹窗
    dialog(id) {
      this.dialogVisible = true
      this.id = id
    },
    // 确认配置司机
    showTime() {
      this.dialogVisible2 = false
      this.isShow = true
    },
    // 配置司机弹窗
    dialog2(row) {
      this.dialogVisible2 = true
      this.id = row.id
      this.truckTypeName = row.truckTypeName
      this.licensePlate = row.licensePlate
      this.status = row.status
      this.allowableLoad = row.allowableLoad
      this.allowableVolume = row.allowableVolume
    },
    // 修改启用停用
    async changeType() {
      const params = this.id
      await this.$axios.put(`https://slwl-api.itheima.net/manager/enable/${this.id}`, params)
        .then(res => {
          this.$message({
            message: '操作成功！',
            type: 'success'
          })
        })
        .catch(err => {
          this.$message({
            message: err,
            type: 'error'
          })
        })
      this.dialogVisible = false
    },
    // 搜索按钮
    async search() {
      if (this.value === `` && this.input === ``) {
        return this.$message({
          message: `搜索项不能为空`,
          type: 'error'
        })
      }
      const arr = []
      this.listLoading = true
      const res = await truckList(this.data)
      for (let index = 0; index < Number(res.data.pages); index++) {
        const { data } = await truckList({ page: index + 1, pageSize: 10 })
        for (let i = 0; i < data.items.length; i++) {
          // 只搜索车辆类型
          if (this.value !== ``) {
            if (data.items[i].truckTypeName === this.value) {
              arr.push(data.items[i])
            }
          }
          // 只搜索车牌号码
          if (this.input !== ``) {
            if (data.items[i].licensePlate === this.input) {
              arr.push(data.items[i])
            }
          }
        }
      }
      this.total = arr.length
      // arr.splice(10, 999999999)
      this.listLoading = false

      this.tableData = arr
      this.fn()
    },
    // 重置按钮
    reset() {
      this.value = ``
      this.input = ``
      this.data.page = 1
      this.data.pageSize = 10
      this.getData()
    },
    // 显示可用车辆
    async workStatus1() {
      const arr = []
      this.listLoading = true
      const res = await truckList(this.data)
      for (let index = 0; index < Number(res.data.pages); index++) {
        const { data } = await truckList({ page: index + 1, pageSize: 10 })
        for (let i = 0; i < data.items.length; i++) {
          if (data.items[i].workStatus === 1) {
            arr.push(data.items[i])
          }
        }
      }
      this.listLoading = false
      this.use = arr.length
      this.tableData = arr
      this.total = this.use
      this.fn()
      // 实现TAG切换
      const one = this.$refs.one
      const two = this.$refs.two
      const the = this.$refs.the
      two.classList.add(`active`)
      one.classList.remove(`active`)
      the.classList.remove(`active`)
    },
    // 显示停用车辆
    async workStatus0() {
      const arr = []
      this.listLoading = true
      const res = await truckList(this.data)
      for (let index = 0; index < Number(res.data.pages); index++) {
        const { data } = await truckList({ page: index + 1, pageSize: 10 })
        for (let i = 0; i < data.items.length; i++) {
          if (data.items[i].workStatus === 0) {
            arr.push(data.items[i])
          }
        }
      }
      this.listLoading = false
      this.unuse = arr.length
      this.tableData = arr
      this.total = this.unuse
      arr.splice(10, 999999999)
      this.fn()
      // 实现TAG切换
      const one = this.$refs.one
      const two = this.$refs.two
      const the = this.$refs.the
      the.classList.add(`active`)
      one.classList.remove(`active`)
      two.classList.remove(`active`)
    },
    // 跳转数据页
    handleCurrentChange(val) {
      this.data.page = val
      this.getData()
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      this.data.pageSize = val
      this.getData()
      if (this.data.page === 1) {
        this.getData(this.data)
      }
    },
    // 高亮样式
    active(k) {
      this.activeCalss = k
      // console.log(this.activeCalss)
    },
    // 新增车辆
    async add() {
      const params = {
        allowableLoad: '',
        allowableVolume: '',
        deviceGpsId: this.form.deviceGpsId,
        licensePlate: this.form.licensePlate,
        truckTypeId: '1575257460197634049',
        truckTypeName: this.form.truckTypeId
      }
      await this.$axios.post(`https://slwl-api.itheima.net/manager/truck`, params)
        .then(res => {
          this.$message({
            message: '操作成功！',
            type: 'success'
          })
        })
        .catch(err => {
          this.$message({
            message: err,
            type: 'error'
          })
        })
      // await truckTypeAdd(this.form).then((res) => {
      //   if (String(res.code) === '200') {
      //     this.$message({
      //       message: '操作成功！',
      //       type: 'success'
      //     })
      //     this.show = false
      //   } else {
      //     this.$message({
      //       message: res.msg,
      //       type: 'error'
      //     })
      //   }
      // })
    },
    // 获取车辆类型下拉
    async getOptions() {
      const { data } = await getCarTypeList()
      for (let index = 0; index < data.length; index++) {
        data[index] = {
          value: `${data[index].name}`,
          label: `${data[index].name}`
        }
        this.options.push(data[index])
      }
      // console.log(this.options)
    },
    // 显示序号和状态
    fn() {
      for (let index = 0; index < this.tableData.length; index++) {
        if (this.tableData[index].workStatus === 1) {
          // 显示可用
          this.tableData[index].workStatus = `可用`
          // 显示序号
          this.tableData[index].brand = (index + 1) + (this.data.page - 1) * this.data.pageSize
        } if (this.tableData[index].workStatus === 0) {
          // 显示停用
          this.tableData[index].workStatus = `停用`
          // 显示序号
          this.tableData[index].brand = (index + 1) + (this.data.page - 1) * this.data.pageSize
        }
      }
    },
    // 获取车辆列表
    async getData() {
      const res = await truckList(this.data)
      this.listLoading = true
      this.tableData = res.data.items
      // console.log(res.data)
      this.total = Number(res.data.counts)
      this.listLoading = false
      this.fn()
      const abc = await getCarTypeStatics()
      this.use = abc.data[1]
      this.unuse = abc.data[0]
      // 实现TAG切换
      const one = this.$refs.one
      const two = this.$refs.two
      const the = this.$refs.the
      one.classList.add(`active`)
      two.classList.remove(`active`)
      the.classList.remove(`active`)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.hidd{
  width: 100%;

  overflow:auto
}
/deep/ .small-padding{
  .cell{
    text-align:left!important
  }
}
.serchBox {
  .el-select {
    width: 300px;
  }
  text-align: left;
  margin-bottom: 20px;
}
.header {
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
  width: 584px;
  height: 131px;
  background-color: #fbfafa;
  flex-wrap: wrap;
  padding: 21px 52px;
  text-align: left;
  .el-input {
    width: 240px;
  }
  div {
    flex: 40%;
  }
  .num,
  .iphone {
    flex: 60%;
  }
  label {
    color: #20232a;
    font-size: 14px;
    font-weight: normal;
  }
  span {
    color: #818693;
    font-size: 14px;
  }
  .carType {
    flex: 60%;
    span {
      display: inline-block;
      text-align: center;
      line-height: 20px;
      width: 41px;
      height: 20px;
      border-radius: 13.5px;
      color: white;
      font-size: 12px;
    }
    .use {
      background: #37c608;
    }
    .noUse {
      background: #808694;
    }
  }
}
.pagination {
  height: 40px;
  margin: 32px 0 44px !important;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px !important;
  color: #20232a !important;
}
.selected-driver {
  margin-top: 18px;
}
/deep/ .el-table {
  overflow: initial;
  .el-table__body-wrapper {
    overflow: initial;
    .cell {
      overflow: initial;
    }
  }
}
/deep/ .pagination {
  margin-top: 20px!important;
  margin-bottom: 0px!important;
  padding-bottom: 0px;
  text-align: center;
}
.box-card {
  width: 1100px;
  margin-left: 25px;
  margin-top: 25px;
  // border-radius: 10px;
  font-size: 14px;
  padding-left: 10px;

}
ul {
  display: flex;
}
li {
  width: 120px;
  height: 100%;
}
a {
  font-size: 14px;
  display: inline-block;
  width: 120px;
  height: 100%;
  line-height: 50px;
  margin-top: 0;
  position: relative;
  top: -20px;
  left: -30px;
  text-align: center;
}
.active {
  background-color: #ffeeeb;
  color: #e15536;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
.vehicle /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.vehicle {
  /deep/ .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  /deep/ .el-dialog__body {
    text-align: center;
    padding: 20px 20px 0px 20px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}

// 下拉框
/deep/ .el-select {
  width: 100%;
}
</style>

<style>
.startconfirm {
  width: 481px;
}
.startconfirm .el-message-box__header {
  padding-bottom: 4px;
}
.startconfirm .el-message-box__container {
  margin-bottom: 15px;
}
.confirm .el-message-box__status.el-icon-warning {
  left: 70px;
}
.confirm.el-message-box__title {
  font-size: 16px;
}
.confirm .el-message-box__message p {
  color: #f92d2d;
  margin-top: 5px;
}
.confirm .el-message-box__btns {
  text-align: center;
  margin-top: 10px;
}
</style>
